﻿using Microsoft.AspNetCore.Mvc;
using System.Security.Claims;

namespace StarsAdmin.Web.Core.Conttrollers
{
    [Produces("application/json")]
    public class BaseController : ControllerBase
    {
        /// <summary>
        /// 当前用户Id
        /// </summary>
        public long CurrentUserId
        {
            get
            {
                if (User != null && User.Claims.ToList().Count > 0)
                {
                    var claim = User.Claims.First(claim => claim.Type == ClaimTypes.NameIdentifier);
                    return claim == null ? 0 : Math.Abs(long.Parse(claim.Value));
                } else
                {
                    return 0;
                }
            }
        }

        /// <summary>
        /// 当前用户Id
        /// </summary>
        public string CurrentUserName
        {
            get
            {
                if (User != null && User.Claims.ToList().Count > 0)
                {
                    var claim = User.Claims.First(claim => claim.Type == ClaimTypes.Name);
                    return claim == null ? string.Empty : claim.Value;
                } else
                {
                    return string.Empty;
                }
            }
        }

        public BaseController()
        {
        }
    }
}